/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.DichVu;

import DTO.DichVu_DTO;
import Define.Define;
import GUI.ThuePhong.JIFDatPhong;
import GUI.Utilities;
import Server.QLKS_Interface;
import java.awt.HeadlessException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Mr
 */
public class JIFThemDichVu extends javax.swing.JInternalFrame {

    private String maDichVu;
    String ServerURL;
    QLKS_Interface QI;

    /**
     * Creates new form JIFThemDichVu
     * @throws java.lang.Exception
     */
    public JIFThemDichVu() throws Exception {
        initComponents();
        this.setSize(880, 560);
        ServerURL = "qlks";
        try {
            QI = (QLKS_Interface) Naming.lookup(ServerURL);
        } catch (NotBoundException | MalformedURLException | RemoteException ex) {
            Logger.getLogger(JIFDatPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        showBangDV();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        tbDichVu = new javax.swing.JTable();
        btThemDV = new javax.swing.JButton();
        btCapNhatDV = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        tfTenDV = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        tfDonGiaDV = new javax.swing.JTextField();
        btXoaDV = new javax.swing.JButton();
        btThoat = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("QUẢN LÝ CÁC DỊCH VỤ");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jScrollPane5.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tbDichVu.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDichVu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDichVuMouseClicked(evt);
            }
        });
        jScrollPane5.setViewportView(tbDichVu);

        btThemDV.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        btThemDV.setText("Thêm Dịch Vụ");
        btThemDV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemDVActionPerformed(evt);
            }
        });

        btCapNhatDV.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Sync.png"))); // NOI18N
        btCapNhatDV.setText("Cập Nhật");
        btCapNhatDV.setEnabled(false);
        btCapNhatDV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatDVActionPerformed(evt);
            }
        });

        jLabel7.setText("Tên Dịch Vụ");

        jLabel8.setText("Đơn giá");

        tfDonGiaDV.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfDonGiaDV.setText("0");

        btXoaDV.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Erase.png"))); // NOI18N
        btXoaDV.setText("Xóa");
        btXoaDV.setEnabled(false);
        btXoaDV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaDVActionPerformed(evt);
            }
        });

        btThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close.png"))); // NOI18N
        btThoat.setText("Thoát");
        btThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 528, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btThemDV)
                        .addGap(19, 19, 19)
                        .addComponent(btXoaDV, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btCapNhatDV)
                        .addGap(18, 18, 18)
                        .addComponent(btThoat)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfTenDV, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, Short.MAX_VALUE)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfDonGiaDV, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(tfTenDV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(tfDonGiaDV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCapNhatDV)
                    .addComponent(btThemDV)
                    .addComponent(btXoaDV)
                    .addComponent(btThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 531, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbDichVuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDichVuMouseClicked
        // TODO add your handling code here:
        int row = tbDichVu.getSelectedRow();
        if (row >= 0) {
            btXoaDV.setEnabled(true);
            btCapNhatDV.setEnabled(true);
            maDichVu = (String) tbDichVu.getValueAt(row, 0);
            String tenDichVu = (String) tbDichVu.getValueAt(row, 1);
            String donGia = tbDichVu.getValueAt(row, 2).toString();
            tfTenDV.setText(tenDichVu);
            tfDonGiaDV.setText(donGia);
        }
    }//GEN-LAST:event_tbDichVuMouseClicked

    private void btThemDVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemDVActionPerformed
        // TODO add your handling code here:
        try {
            luuDichVu();
            showBangDV();
        } catch (Exception ex) {
            Logger.getLogger(JIFThemDichVu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btThemDVActionPerformed

    private void btCapNhatDVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatDVActionPerformed
        // TODO add your handling code here:
        String tenDichVu;
        tenDichVu = tfTenDV.getText();
        if (tenDichVu.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên dịch vụ!");
            return;
        }
        Float donGia;
        try {
            donGia = Float.parseFloat(tfDonGiaDV.getText());
            if (donGia < 0 || donGia > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
                tfDonGiaDV.setText("0");
                return;
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!!");
            tfDonGiaDV.setText("0");
            return;
        }

        try {
            if (QI.KiemTraTonTai(Define.YC_LAYDS_DICHVU, maDichVu)) {
                JOptionPane.showMessageDialog(rootPane, "Dịch vụ đang được đặt không thể cập nhật!");
                return;
            } else {
            }
            DichVu_DTO dv = new DichVu_DTO();
            dv.setMaDichVu(maDichVu);
            dv.setTenDichVu(tenDichVu);
            dv.setDonGia(donGia.toString());
            //DichVu_BUS.Update(dv);
            QI.UpdateDichVu(dv);
            showBangDV();
        } catch (Exception ex) {
            Logger.getLogger(JIFThemDichVu.class.getName()).log(Level.SEVERE, null, ex);
        }
        tfTenDV.setText("");
        tfDonGiaDV.setText("0");
        btXoaDV.setEnabled(false);
        btCapNhatDV.setEnabled(false);
    }//GEN-LAST:event_btCapNhatDVActionPerformed

    private void btXoaDVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaDVActionPerformed
        // TODO add your handling code here:
        int row = this.tbDichVu.getSelectedRow();
        int xoa = JOptionPane.showConfirmDialog(rootPane, "Bạn có chắc chắn muốn xóa?");
        if (xoa != 0) {
            return;
        }
        try {
            String maDichVuXoa = (String) this.tbDichVu.getValueAt(row, 0).toString();
            //if (DichVu_BUS.kiemTraDichVuCoTrongCTPhieuDichVu(maDichVuXoa)) {
            if (QI.KiemTraTonTai(Define.YC_LAYDS_DICHVU, maDichVuXoa)) {
                JOptionPane.showMessageDialog(rootPane, "Dịch vụ đang được đặt không thể xóa!");
                return;
            } else {
            }
            //DichVu_BUS.Delete(maDichVuXoa);
            QI.DeleteDichVu(maDichVuXoa);
            showBangDV();
        } catch (Exception e) {
        }
        tfTenDV.setText("");
        tfDonGiaDV.setText("0");
        btXoaDV.setEnabled(false);
        btCapNhatDV.setEnabled(false);
    }//GEN-LAST:event_btXoaDVActionPerformed

    private void btThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCapNhatDV;
    private javax.swing.JButton btThemDV;
    private javax.swing.JButton btThoat;
    private javax.swing.JButton btXoaDV;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable tbDichVu;
    private javax.swing.JTextField tfDonGiaDV;
    private javax.swing.JTextField tfTenDV;
    // End of variables declaration//GEN-END:variables

    private void luuDichVu() throws Exception {
        String tenDichVu;
        tenDichVu = tfTenDV.getText();
        if (tenDichVu.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa nhập tên dịch vụ!");
            return;
        }
        Float donGia;
        try {
            donGia = Float.parseFloat(tfDonGiaDV.getText());
            if (donGia < 0 || donGia > 10000000) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!");
                tfDonGiaDV.setText("0");
                return;
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số từ 0 đến 10.000.000!!");
            tfDonGiaDV.setText("0");
            return;
        }
        String maDV;
        maDV = maDichVu();
        DichVu_DTO dv = new DichVu_DTO();
        dv.setMaDichVu(maDV);
        dv.setTenDichVu(tenDichVu);
        dv.setDonGia(donGia.toString());
        //DichVu_BUS.Insert(dv);
        QI.InsertDichVu(dv);
        JOptionPane.showMessageDialog(rootPane, "Thêm dịch vụ thành công!");
    }

    private void showBangDV() throws Exception {
        //tbDichVu.setModel(DichVu_BUS.layBangDichVu());
        tbDichVu.setModel(QI.LoadTableModel(Define.YC_LAYDS_DICHVU));
    }

    private String maDichVu() throws Exception {
        //String lastID = DichVu_BUS.layMaCuoiCung();
        String lastID = QI.LayMaMax(Define.YC_LAYMAMAX_DICHVU);
        String nextID = null;
        try {
            if (lastID != null) {
                nextID = Utilities.taoMaTuDong(lastID, 3);
            } else {
                nextID = Utilities.taoMaTuDong("MDV0000", 3);
            }
        } catch (Exception e) {
        }
        return nextID;
    }
}
